using System;
using Net.Common;
using UnityEngine;

/****************************************************
// 功能：UI窗口基类
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2024/12/29 11:38:15
*****************************************************/

public partial class BaseWindow : MonoBehaviour
{
    // 窗口ID
    private WindowName id;

    // 关闭窗口的回调函数
    private Action<WindowName> closeHandle;
    
    // 是否是弹窗
    public bool isPop;

    public void SetWindowsState(WindowName id, bool isActive = true, Action<WindowName> closeHandle = null)
    {
        if (gameObject.activeSelf != isActive)
        {
            
            SetActive(gameObject, isActive);
        }
        
        this.id = id;
        this.closeHandle = closeHandle;

        if (isActive)
        {
            InitWindows();
        }
        else
        {
            ClearWindows();
        }
    }

    public void Open(object arg, bool isPop = false)
    {
        this.isPop = isPop;
        SetActive(gameObject);
        OnOpen(arg);
    }

    /// <summary>
    /// 窗口初始化调用
    /// </summary>
    protected virtual void InitWindows()
    {
        resource = ResourceManager.Instance;
        net = NetManager.Instance;
        model = ModelManager.Instance;
        audioManager = AudioManager.Instance;
        ui = UIManager.Instance;
    }

    
    /// <summary>
    /// 虚函数，在窗口打开的时候调用
    /// </summary>
    protected virtual void OnOpen(object arg) { }

    protected virtual void ClearWindows()
    {
        if (!isPop) { closeHandle?.Invoke(id); }
    }
    
    public void SendByTcp(RequestType type, object data)
    {
        net.SendByTcp(type, data);
    }

    public void SendByKcp(RequestType type, object data)
    {
        net.SendByKcp(type, data, model.userInfo.channelId);
    }
    
    public void SendByWebSocket(RequestType type, object data)
    {
        net.SendByWebSocket(type, data);
    }
}